package com.plian.system.controller.imTbjg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.*;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.Behavioral;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.im.ProjectInfo;
import com.plian.system.entity.imtbjg.ImProjectInfoTbjg;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.Logic;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.im.IProjectInfoService;
import com.plian.system.service.imTbjg.IImProjectInfoTbjgService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.im.ProjectInfoVO;
import com.plian.system.vo.imTbjg.ImProjectInfoTbjgVO;
import com.plian.system.wrapper.im.ProjectInfoWrapper;
import com.plian.system.wrapper.imTbjg.ImProjectInfoTbjgWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * 控制器
 *
 * @author
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("api/projectinfoTbjg")
@Api(value = "ImProjectInfoTbjgController", tags = "项目信息 控制器")
public class ImProjectInfoTbjgController extends BaseController {

	private IImProjectInfoTbjgService iImProjectInfoTbjgService;

	private ImProjectInfoTbjgWrapper imProjectInfoTbjgWrapper;
	private ProjectInfoWrapper projectInfoWrapper;
	private IProjectInfoService iProjectInfoService;

	private FormWorkflowService formWorkflowService;
	/**
	 * 详情
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "详情")
	@Behavioral
	public R<ImProjectInfoTbjgVO> detail(@RequestBody ImProjectInfoTbjg imProjectInfoTbjg) {
		ImProjectInfoTbjg detail = iImProjectInfoTbjgService.getOne(Condition.getQueryWrapper(imProjectInfoTbjg));
		ImProjectInfoTbjgVO vo=imProjectInfoTbjgWrapper.entityToVO(detail);
		if(vo!=null&&vo.getProjectId()!=null){
			ProjectInfo projectInfo=iProjectInfoService.getById(vo.getProjectId());
			if(projectInfo!=null&&projectInfo.getId()!=null){
				ProjectInfoVO projectInfoVO=projectInfoWrapper.entityToVO(projectInfo);
				if(projectInfoVO!=null){
					vo.setProjectInfoVO(projectInfoVO);
				}
			}
		}
		return R.data(vo);
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	@ApiOperation(value = "分页")
	public ListResult<ImProjectInfoTbjgVO> list(@RequestBody Query query) {
		List<SearchEntity> projectEntityList = query.getSearch();


		QueryWrapper<ProjectInfo> projectInfoQueryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new ProjectInfo()), projectEntityList
		);
		projectInfoQueryWrapper.eq("is_special_supervise",2);
		projectInfoQueryWrapper.eq("status",3);
		List<ProjectInfo> projectInfoList= iProjectInfoService.list(projectInfoQueryWrapper);

		List<Long> longList=new ArrayList<>();
		if(projectInfoList!=null&&projectInfoList.size()>0){
			for (ProjectInfo projectInfo: projectInfoList) {
				longList.add(projectInfo.getId());
			}
		}



		List<SearchEntity> entityList=new ArrayList<>();
//		SearchEntity searchEntity=new SearchEntity();
//		searchEntity.setField("project_id");
//		searchEntity.setCondition(Logic.in);
//		searchEntity.setValue(longList);
//		entityList.add(searchEntity);
		query.setSearch(entityList);
		query.setDescs("create_time");

		QueryWrapper<ImProjectInfoTbjg> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new ImProjectInfoTbjg()), entityList
		);
		TokenSearch<ImProjectInfoTbjg> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);
		if(longList!=null&&longList.size()>0){
			queryWrapper.in("project_id",Func.toLongList(StringUtils.join(longList, StringPool.COMMA)));
		}else{
			queryWrapper.eq("project_id",null);

		}

		IPage<ImProjectInfoTbjg> pages = iImProjectInfoTbjgService.page(Condition.getPage(query), queryWrapper);
		List<ImProjectInfoTbjgVO> imProjectInfoTbjgVOs= imProjectInfoTbjgWrapper.entityToVO(pages.getRecords());
		ListResult<ImProjectInfoTbjgVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		if(imProjectInfoTbjgVOs!=null&&imProjectInfoTbjgVOs.size()>0){
			for (ImProjectInfoTbjgVO imProjectInfoTbjgVO: imProjectInfoTbjgVOs) {
				if(imProjectInfoTbjgVO!=null&&imProjectInfoTbjgVO.getProjectId()!=null){
					ProjectInfo projectInfo=iProjectInfoService.getById(imProjectInfoTbjgVO.getProjectId());
					if(projectInfo!=null&&projectInfo.getId()!=null){
						ProjectInfoVO projectInfoVO=projectInfoWrapper.entityToVO(projectInfo);
						if(projectInfoVO!=null){
							imProjectInfoTbjgVO.setProjectInfoVO(projectInfoVO);
						}
					}
				}
			}
		}
		listResult.setList(imProjectInfoTbjgVOs);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	/**
	 * 所有列表
	 */
	@PostMapping("/listAll")
	@ApiOperation(value = "所有列表")
	public ListResult<ImProjectInfoTbjg> listAll(@RequestBody Query query) {








		List<SearchEntity> entityList = query.getSearch();
		QueryWrapper<ImProjectInfoTbjg> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new ImProjectInfoTbjg()), entityList
		);
		queryWrapper.eq("create_company_id", TokenUtil.getTokenOrgId());
		List<ImProjectInfoTbjg> list = iImProjectInfoTbjgService.list(queryWrapper);
		ListResult<ImProjectInfoTbjg> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(list);
		listResult.setTotal(list.size());
		return listResult;
	}
	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@ApiOperation(value = "新增或修改")
	@LogOperation("特别监管项目-提交")
	public R<ImProjectInfoTbjg> submit(@Valid @RequestBody ImProjectInfoTbjg imProjectInfoTbjg) {
		boolean result = iImProjectInfoTbjgService.submit(imProjectInfoTbjg);
		HashMap<String, Object> processVariable = new HashMap<>();
		formWorkflowService.startProcessAndSimulate(String.valueOf(imProjectInfoTbjg.getId()),imProjectInfoTbjg.getFormType(),processVariable);

		return result?R.data(imProjectInfoTbjg) : new R(CommonCode.FAIL);


	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除")
	@LogOperation("特别监管项目-删除")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return iImProjectInfoTbjgService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}


}
